package com.qysxy.provideruser.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.qysxy.api.user.UserDto;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;


/**
 * Created by SuLong on 2018/4/16/016 14:58.
 */
@RestController
public class IndexController implements InitializingBean {

    private Map<String, Boolean> map = new HashMap<>(); // 当前map存储的状态值属于当前应用，如果放在负载均衡中会有不一样的结果，所以对于类级别的变量建议使用redis存储

    @RequestMapping("/home")
    @HystrixCommand(fallbackMethod = "getFallback")
    public UserDto discover() {
        UserDto dto = new UserDto();
        dto.setName("邓壮壮范德萨范德萨");
        int i = 1/0;
        return dto;
    }

    @RequestMapping("/order")
    public String order() {
        map.put("name", !map.get("name"));
        if (map.get("name") == true) {
            return "下单成功";
        } else  {
            return "下单失败";
        }
    }

    @RequestMapping("/result")
    public String result() {
        if (map.get("name") == true) {
            return "下单成功";
        } else  {
            return "下单失败";
        }
    }

    public UserDto getFallback() {
        UserDto dto = new UserDto();
        dto.setName("发生错误，服务端进行了降级处理");
        return dto;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        map.put("name", false);
    }
}
